/**
 *    @file       comdef.h
 *
 *    @brief      
 *    @details
 *
 *    @version    1.0
 *    @date       12/08/2012 05:25:52 PM
 *
 *    @author     Theophilus, wangtf418@gmail.com
 */
#ifndef _BF_COMDEF_H_
#define _BF_COMDEF_H_
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <string>
#include <sys/types.h> 
#include <pthread.h>

/**
 * define basic integer
 */
typedef char bf_int8_t;
typedef short bf_int16_t;
typedef int bf_int32_t;
typedef long long bf_int64_t;

typedef unsigned char bf_uint8_t;
typedef unsigned short bf_uint16_t;
typedef unsigned int bf_uint32_t;
typedef unsigned long long bf_uint64_t;


typedef char bf_char_t;
typedef short bf_short_t;
typedef int bf_int_t;
typedef long bf_long_t;
typedef long long bf_llong_t;

typedef unsigned char bf_uchar_t;
typedef unsigned short bf_ushort_t;
typedef unsigned int bf_uint_t;
typedef unsigned long bf_ulong_t;
typedef unsigned long long bf_ullong_t;

enum bf_basic_wordlen_t
{
	BF_BASIC_WLEN0 = 0,
	BF_BASIC_WLEN8 = 1,
	BF_BASIC_WLEN16 = 2,
	BF_BASIC_WLEN32 = 4,
	BF_BASIC_WLEN64 = 8
};

enum bf_max_decimallen_t
{
	BF_MAX_INT8_DECIMALLEN = 4,
	BF_MAX_INT16_DECIMALLEN = 6,
	BF_MAX_INT32_DECIMALLEN = 11,
	BF_MAX_INT64_DECIMALLEN = 20,

	BF_MAX_UINT8_DECIMALLEN = 3,
	BF_MAX_UINT16_DECIMALLEN = 5,
	BF_MAX_UINT32_DECIMALLEN = 10,
	BF_MAX_UINT64_DECIMALLEN = 20
};

/**
 * define basic float
 */
typedef float bf_float32_t;
typedef double bf_float64_t;

typedef float bf_float_t;
typedef double bf_double_t;

/** 
 * define other basic variable
 */
typedef bool bf_bool_t;
typedef size_t bf_size_t;
typedef time_t bf_time_t;
typedef va_list bf_valist_t;
typedef pid_t bf_pid_t;
typedef pthread_t bf_pthread_t;

/**
 *  define const macro
 */
#define BF_ZERO 0
#define BF_POSITIVE_ONE 1
#define BF_NEGATIVE_ONE -1

#define BF_OK BF_ZERO
#define BF_ERROR BF_NEGATIVE_ONE

#define BF_TRUE BF_POSITIVE_ONE
#define BF_FALSE BF_ZERO

#define BF_SUCC BF_POSITIVE_ONE
#define BF_FAIL BF_ZERO

#define BF_SIZE_OF_INT8  1
#define BF_SIZE_OF_INT16 2
#define BF_SIZE_OF_INT32 4
#define BF_SIZE_OF_INT64 8

#define BF_INT8_MIN SCHAR_MIN
#define BF_INT8_MAX SCHAR_MAX
#define BF_INT16_MIN SHRT_MIN
#define BF_INT16_MAX SHRT_MAX
#define BF_INT32_MIN INT_MIN
#define BF_INT32_MAX INT_MAX
#define BF_INT64_MIN LLONG_MIN
#define BF_INT64_MAX LLONG_MAX

#define BF_UINT8_MIN 0
#define BF_UINT8_MAX UCHAR_MAX
#define BF_UINT16_MIN 0
#define BF_UINT16_MAX USHRT_MAX
#define BF_UINT32_MIN 0
#define BF_UINT32_MAX UINT_MAX
#define BF_UINT64_MIN 0
#define BF_UINT64_MAX ULLONG_MAX

#define BF_CHAR_MIN SCHAR_MIN
#define BF_CHAR_MAX SCHAR_MAX
#define BF_SHORT_MIN SHRT_MIN
#define BF_SHORT_MAX SHRT_MAX
#define BF_INT_MIN INT_MIN
#define BF_INT_MAX INT_MAX
#define BF_LONG_MIN LONG_MIN
#define BF_LONG_MAX LONG_MAX
#define BF_LLONG_MIN LLONG_MIN
#define BF_LLONG_MAX LLONG_MAX

#define BF_UCHAR_MIN 0
#define BF_UCHAR_MAX UCHAR_MAX
#define BF_USHORT_MIN 0
#define BF_USHORT_MAX USHRT_MAX
#define BF_UINT_MIN 0
#define BF_UINT_MAX UINT_MAX
#define BF_ULONG_MIN 0
#define BF_ULONG_MAX ULONG_MAX
#define BF_ULLONG_MIN 0
#define BF_ULLONG_MAX ULLONG_MAX

#define BF_SIZE_MIN 0
#define BF_SIZE_MAX ((bf_size_t)-1)

#ifndef INVALID_SOCKET
#define INVALID_SOCKET -1
#endif

#ifndef INVALID_TIME
#define INVALID_TIME -1
#endif

#define BF_NAME_MAX NAME_MAX
#define BF_PATH_MAX PATH_MAX

#define BF_SHA1_HASH_LEN 20
#define BF_SHA1_HASH_HEXLEN 40
#define BF_MD5_HASH_LEN 16
#define BF_MD5_HASH_HEXLEN 32
#define BF_AES_BLOCK_LEN 16
#define BF_AES_BLOCK_HEXLEN 32

#define BF_BITS8_BYTES 1
#define BF_BITS16_BYTES 2
#define BF_BITS32_BYTES 4
#define BF_BITS64_BYTES 8
#define BF_BITS128_BYTES 16
#define BF_BITS192_BYTES 24
#define BF_BITS256_BYTES 32

/**
 *  define process macro
 */
#define IF_OK_RETURN(cond) if ((cond) == BF_OK) {return BF_OK;}
#define IF_ERR_RETURN(cond) if ((cond) != BF_OK) {return BF_ERROR;}

/**
 * define basic structure
 */
#define BF_EMPTY_STRING  bf_string_t(NULL, 0)
struct bf_string_t
{
	bf_char_t *m_str;
	bf_size_t m_len;

	bf_string_t() : m_str(NULL), m_len(0) {}
	bf_string_t(bf_char_t *str, bf_size_t len) : m_str(str), m_len(len){}
	bf_string_t(const bf_string_t &str) : m_str(str.m_str), m_len(str.m_len) {}
	bf_bool_t operator==(const bf_string_t &str)
	{
		if (m_str == NULL || str.m_str == NULL || m_len != str.m_len) return false;
		return (memcpy(m_str, str.m_str, str.m_len));
	}
	std::string to_string()
	{
		return (m_str == NULL || m_len == 0)? "":std::string(m_str, m_len);
	}
};

struct bf_tm_t
{
	bf_int_t m_year;		//  Year >= 1900.
	bf_int_t m_month;		//  Month.   [1-12]
	bf_int_t m_day;			//  Day.     [1-31]
	bf_int_t m_hour;		//  Hours.   [0-23]
	bf_int_t m_min;			//  Minutes. [0-59]
	bf_int_t m_sec;			//  Seconds. [0-60] (1 leap second)
};


#endif

